package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleMapper;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoleServiceImpl implements RoleService {
    @Override
    public List<Role> findAll() {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        List<Role> list = roleMapper.findAll();

        // 释放资源
        MyBatisUtils.release(sqlSession);
        return list;
    }


    @Override  //查詢所有權限名
    public List<Permission> finbAllPermission() {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        List<Permission> list = roleMapper.finbAllPermission();
        // 释放资源
        MyBatisUtils.release(sqlSession);
        return list;

    }

    @Override
    public PageInfo<Role> findByPage(Integer pageNum, Integer pageSize) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        PageHelper.startPage(pageNum, pageSize);
        //对这个查询进行增强 把实体类进行增强  增加属性 list<permission>集合
        List<Role> list = roleMapper.findAll();
        // 释放资源
        MyBatisUtils.release(sqlSession);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Role role) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        //添加角色
        roleMapper.save(role);
        //添加角色權限中間表
        Integer[] ids = role.getIds();
        //獲取id   前提需要開啟外鍵返回策略
        Integer roleId = role.getId();
        for (Integer pid : ids) {
            roleMapper.saveRole_Permission(roleId,pid);
        }

        // 释放资源
        MyBatisUtils.release(sqlSession);
    }

    @Override
    public Role findById(Integer id) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

        Role role = roleMapper.findById(id);

        // 释放资源
        MyBatisUtils.release(sqlSession);
        return role;
    }

    @Override
    public void update(Role role) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        //1.先修改角色
        roleMapper.update(role);
        //2.在修改权限   修改前需要全部删除 根据roleid
        //根据角色id删除之前所有的角色的权限
        Integer roleId = role.getId();
        System.out.println("要删除的用户id是+"+roleId);
        //修改之前先删除 角色权限
       roleMapper.deletePermissionByRid(roleId);
        //重新添加用户角色
        Integer[] ids = role.getIds();
        if(ids!=null){
            for (Integer id : ids) {
                roleMapper.saveRole_Permission(roleId,id);
            }
        }



        // 释放资源
        MyBatisUtils.release(sqlSession);
    }

    @Override
    public void delete(Integer id) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        //如果有user拥有这个角色 还需要删除 和user的中间表的数据
       // roleMapper.deleteUser_RoleByRid(id);
        //先删除权限的外键
        roleMapper.deletePermissionByRid(id);
        //再删除角色
        roleMapper.delete(id);

        // 释放资源
        MyBatisUtils.release(sqlSession);
    }


}
